
/**
 * 应用程序权限菜单相关Store
 */
 import utils from '@/common/utils';
 import router from "@/router/index";
 import { getMenuList } from '@/api/system';
 
 const componentPages = [
    //  { key: 'index', page: () => import('@/views/home/index') },
    //  { key: 'orginfo', page: () => import('@/views/system/orginfo') },
    //  { key: 'empinfo', page: () => import('@/views/system/empinfo') },
    //  { key: 'sysrole', page: () => import('@/views/system/sysrole') },
    //  { key: 'sysuser', page: () => import('@/views/system/sysuser') },
    //  { key: 'onlineuser', page: () => import('@/views/system/onlineuser') },
    //  { key: 'basedic', page: () => import('@/views/system/dicinfo') },
    //  { key: 'syslog', page: () => import('@/views/system/loginfo') },
    //  { key: 'customer', page: () => import('@/views/basic/customer') },
    //  { key: 'mtrparam', page: () => import('@/views/basic/mtrparam') },
    //  { key: 'procsource', page: () => import('@/views/basic/procsource') },
    //  { key: 'matchinecell', page: () => import('@/views/basic/matchinecell/index') },
    //  { key: 'procedure', page: () => import('@/views/basic/procedure/index') },
    //  { key: 'materialmodel', page: () => import('@/views/basic/materialmodel/index') },
    //  { key: 'calendar', page: () => import('@/views/basic/calendar/index') },
    //  { key: 'resourcecalendar', page: () => import('@/views/basic/resourcecalendar/index') },
    //  { key: 'product', page: () => import('@/views/basic/product/index') },
    //  { key: 'variety', page: () => import('@/views/basic/variety/index') },
    //  { key: 'processinspection', page: () => import('@/views/basic/processinspection/index') },   //新增检查
    //  { key: 'busineseorder', page: () => import('@/views/businese/order/index') },
    //  { key: 'busineseworksheet1', page: () => import('@/views/businese/worksheet/index') },
    //  { key: 'businesesourceapply', page: () => import('@/views/businese/sourceapply/index') },
    //  { key: 'busineseprorecord', page: () => import('@/views/businese/productionrecord/index') },
    //  { key: 'busineseschedule', page: () => import('@/views/businese/schedule/index') },
    //  { key: 'businesequerybyorder', page: () => import('@/views/businese/query/order') },
    //  { key: 'businesequerybycell', page: () => import('@/views/businese/query/cell') },
    //  { key: 'businesequerybygantt', page: () => import('@/views/businese/query/mygantt') },      //新增甘特
 ];
 
 const getDefaultState = () => {
     return {
         // 功能菜单列表，树形结构列表
         // { Auth: true, Key: 'index', LinkUrl: '/index', Title: '首页', Icon: 'el-icon-house', Children: [] }
         menulist: [],
         // 功能菜单路由列表，不包含系统内置路由
         // { name: 'index', path: '/index', meta: { auth: true, title: '首页', icon: 'el-icon-house', show: true }, component: 'page',}
         routes: []
     };
 };
 
 const state = getDefaultState();
 const mutations = {
     RESET_STATE: (state) => {
         state.routes.forEach(x => { if (router.hasRoute(x.name)) router.removeRoute(x.name); });
         Object.assign(state, getDefaultState());
     },
     CHANGE_STATE: (state, { key, value }) => { if (Object.prototype.hasOwnProperty.call(state, key)) { state[key] = value; } },
     SET_MENULIST: (state, menus) => {
         if (utils.isEmpty(menus) || menus.length < 1) {
             Object.assign(state, getDefaultState());
             return;
         }
         state.menulist = menus;
         // 转化路由数据
         const tmpArr = [];
         let tmpPage = null;
         //递归
         const deepFunc = (x) => {
             x.forEach(y => {
                 if (Object.prototype.hasOwnProperty.call(y, 'children') &&
                     !utils.isEmpty(y.children) &&
                     y.children.length > 0) {
                     deepFunc(y.children);
                 } else {
                     tmpPage = componentPages.find(p => p.key === y.fmodulekey);
                     if (!utils.isEmpty(tmpPage)) {
                         tmpArr.push({
                             name: y.fmodulekey,
                             path: y.linkurl,
                             meta: {
                                 auth: y.auth ?? false,
                                 title: y.fmodulename ?? '',
                                 icon: y.icon ?? '',
                                 show: true
                             },
                             component: tmpPage.page
                         });
                     }
                 }
             });
         };
         deepFunc(menus);
         state.routes = tmpArr;
         state.routes.forEach(x => { if (!router.hasRoute(x.name)) router.addRoute('container', x); });
     }
 };
 
 const actions = {
     // reset state
     resetState ({ commit }) { commit('RESET_STATE'); },
     // change state
     changeState ({ commit }, data) { commit('CHANGE_STATE', data); },
     // init menus/routes
     setMenuList ({ commit, state }) { commit('SET_MENULIST', state.menulist); },
     //#############3get menulist获取左侧的菜单栏
     getMenuList ({ commit }) {
         return new Promise((resolve, reject) => {
             getMenuList().then(response => {
                 try {
                     // const { data } = response;
                     const data = response;
                     if (utils.isEmpty(data) || data.length < 1) {
                         commit('RESET_STATE');
                         throw new Error('无可用功能，请联系管理员');
                     }
                     commit('SET_MENULIST', data);
                     resolve(data);
                 } catch (error) { reject(error); }
             }).catch(error => { reject(error); });
         });
     }
 };
 
 export default { namespaced: true, state, mutations, actions };